﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Security.Claims;
using WZN_shop.Models.Database;
using WZN_shop.service;

namespace WZN_shop.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class CarControllers : ControllerBase
    {
        private readonly IJWTService _jwtService;
        private readonly WZN_ShopContext _db;
        public CarControllers(IJWTService jwtService, WZN_ShopContext db)
        {
            _jwtService = jwtService;
            _db = db;
        }


        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="Goodid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string AddCar(int Goodid)
        {
            var userId = int.Parse(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            if (_db.ShopCars.FirstOrDefault(x => x.UserId == userId && x.GoodId == Goodid) != null)
            {
                return "该商品已在购物车";
            }
            ShopCar car=new ShopCar();
            car.UserId = userId;
            car.GoodId = Goodid;
            car.Count = 1;
            
            _db.ShopCars.Add(car);
            if (_db.SaveChanges() > 0)
            {
                return "加入购物车成功";
            }else
            {
                return "加入购物车失败";
            }
        }


        /// <summary>
        /// 可编辑购物车商品数量
        /// </summary>
        /// <param name="goodid"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string ChangeCarCount(int goodid,int count)
        {
            var userId = int.Parse(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            var car=_db.ShopCars.FirstOrDefault(x=>x.UserId== userId&&x.GoodId==goodid);
            if (car == null)
            {
                return "购物车中不存在该商品";
            }
            car.Count=count;
            if (_db.SaveChanges() > 0)
            {
                return "修改成功";
            }
            else
            {
                return "修改失败";
            }
            
        }


        /// <summary>
        /// 删除购物车商品
        /// </summary>
        /// <param name="goodid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public string DetalCar(int goodid)
        {
            var userId = int.Parse(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            var car=_db.ShopCars.FirstOrDefault(x=>x.UserId == userId&&x.GoodId==goodid);
            if(car == null)
            {
                return "您购物车中不存在该商品";
            }
            else
            {
            _db.Remove(car);
            _db.SaveChanges();
                return "删除成功";

            }


        }

        /// <summary>
        /// 查询购物车商品（查询购物车商品列表，并且返回购物车总价）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public decimal TotalCar()
        {
            var userId = int.Parse(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            //自定义列表，多表查询用join()
            var list = _db.ShopCars.Where(x => x.UserId == userId).Join(_db.Goods, x => x.GoodId, z => z.GoodId, (x, z) => new 
            {
                price = z.GoodPrice,
                count=x.Count
            }).ToList();
            //使用sum进行逐一计算
            return list.Sum(item=>item.price*item.count);
            
        }
    }
}
